package com.atguigu.gulimall.thirdpart.controller.wechat.service.impl;

import com.atguigu.gulimall.thirdpart.controller.wechat.ConstantsPlaceholder;
import com.atguigu.gulimall.thirdpart.controller.wechat.WeChatConfig;
import com.atguigu.gulimall.thirdpart.controller.wechat.WeChatException;
import com.atguigu.gulimall.thirdpart.controller.wechat.entity.UserAccessToken;
import com.atguigu.gulimall.thirdpart.controller.wechat.entity.WeChatUserInfo;
import com.atguigu.gulimall.thirdpart.controller.wechat.service.AuthorizeService;
import com.atguigu.gulimall.thirdpart.controller.wechat.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @author zhangxueyou
 * @date 2020/10/2-21:39
 */
@Slf4j
@Service
public class AuthrorizeServiceImpl implements AuthorizeService {
    // https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&r
    // edirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=
    // STATE#wechat_re

    @Override
    public String genSnsapiBaseUrl(String appId, String redirectUri, String state) {

        try {
            String apiUrl = WeChatConfig.getUrl("authorize.get.code")
                    .replace(ConstantsPlaceholder.APPID, appId)
                    .replace(ConstantsPlaceholder.REDIRECT_URI, URLEncoder.encode(redirectUri, "UTF-8"))
                    .replace(ConstantsPlaceholder.SCOPE, "snsapi_base")
                    .replace(ConstantsPlaceholder.STATE, state);

            return apiUrl;
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String genSnsapiUserInfo(String appId, String redirectUri, String state) {
        try {
            String apiUrl = WeChatConfig.getUrl("authorize.get.code")
                    .replace(ConstantsPlaceholder.APPID, appId)
                    .replace(ConstantsPlaceholder.REDIRECT_URI, URLEncoder.encode(redirectUri, "UTF-8"))
                    .replace(ConstantsPlaceholder.SCOPE, "snsapi_userinfo")
                    .replace(ConstantsPlaceholder.STATE, state);
            return apiUrl;
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public UserAccessToken getUserAcsTokenByCode(String appId, String appsecret, String code) {

        String apiUrl = WeChatConfig.getUrl("useraccesstoken.get")
                .replace(ConstantsPlaceholder.APPID, appId)
                .replace(ConstantsPlaceholder.SECRET, appsecret)
                .replace(ConstantsPlaceholder.CODE, code);
        UserAccessToken userAccessToken = HttpUtils.get(apiUrl, UserAccessToken.class);
        log.info("======请求token==========================url[url={}]", apiUrl);
        if (userAccessToken.isSuccess()) {
            return userAccessToken;
        } else {
            throw new WeChatException("get useraccesstoken error " + userAccessToken.toErrorString());
        }
    }

    @Override
    public UserAccessToken refreshUserAcsToken(String appid, String grant_type, String refresh_token) {
        return null;
    }

    @Override
    public WeChatUserInfo getWeChatUnSubscribeUserInfo(String accesstoken, String openid) {
        String apiUrl = WeChatConfig.getUrl("useraccesstoken.get.userinfo")
                .replace(ConstantsPlaceholder.ACCESS_TOKEN, accesstoken)
                .replace(ConstantsPlaceholder.OPENID, openid);

            WeChatUserInfo weChatUserInfo = HttpUtils.get(apiUrl, WeChatUserInfo.class);
            if (weChatUserInfo.isSuccess()) {
                return weChatUserInfo;
            }
            return weChatUserInfo;

    }

    @Override
    public WeChatUserInfo getWeChatSubscribeUserInfo(String accesstoken, String openid) {
        return null;
    }

    @Override
    public UserAccessToken checkUserToken(String accesstoken, String openid) {
        return null;
    }
}
